﻿namespace Autorent.Desktop.Modules.Addresses.Services
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using Autorent.Desktop.Model;
    using Autorent.Desktop.Model.AutorentMngrServiceReference;


    public class CountriesRepository : AutorentEntitiesContext, ICountriesRepository
    {
        public ObservableCollection<Country> GetCountries()
        {
            try
            {
                return new ObservableCollection<Country>(Context.Countries);
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }
        }


        public void AddCountry(Country country)
        {
            Context.AddToCountries(country);
            try
            {
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }
        }

        public void UpdateCountry(Country country)
        {
            Context.UpdateObject(country);
            Context.SaveChanges();
        }

        public void DeleteCountry(Country country)
        {
            try
            {
                Context.DeleteObject(Context.Countries.Where(c => c.Country_ID.Equals(country.Country_ID)));
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while deleting object", ex.InnerException);
            }
        }
    }
}
